package com.example.bili.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bili.R;
import com.example.bili.adapter.d_abstractAdapter;
import com.example.bili.allbean.details_avBean;
import com.example.bili.allbean.details_avBean2;
import com.example.bili.allbean.details_avBean3;
import com.example.bili.allbean.details_relationBean;
import com.example.bili.base.BaseFragment;
import com.example.bili.module.OkHttp;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

import static com.example.bili.internet.connectors.AV;

/**
 * Created by Administrator on 2017/3/12.
 */

public class d_abstract extends BaseFragment implements OkHttp.DataCallBack {

    private TextView title;
    private TextView b_number;
    private TextView p_number;
    private TextView number_fx;
    private TextView number_sc;
    private TextView number_tb;
    private TextView name;
    private SimpleDraweeView img;
    private TagFlowLayout tagflowlayout;
    private RecyclerView recy;
    private details_avBean3 details_bavean3;
    private details_avBean2 details_avbean2;
    private details_avBean details_avbean;
    private ArrayList<details_relationBean> relationlistbean;

    @Override
    protected void initview(View view) {
         title = (TextView) view.findViewById(R.id.d_ab_title_tv);
         b_number = (TextView) view.findViewById(R.id.d_ab_recyitem_bnum);
         p_number = (TextView) view.findViewById(R.id.d_ab_recyitem_pnum);
         number_fx = (TextView) view.findViewById(R.id.d_ab_number_fx);
         number_tb = (TextView) view.findViewById(R.id.d_ab_number_tb);
         number_sc = (TextView) view.findViewById(R.id.d_ab_number_sc);
         name = (TextView) view.findViewById(R.id.d_ab_abstractname);
         img = (SimpleDraweeView) view.findViewById(R.id.d_ab_abstractimg);
         tagflowlayout = (TagFlowLayout) view.findViewById(R.id.d_av_tagflowlayout);
         recy = (RecyclerView) view.findViewById(R.id.d_av_recy);
    }

    @Override
    protected void m_onCreateView() {
        OkHttp.getAsync(AV+"&aid="+getActivity().getIntent().getStringExtra("avid"),this);

    }

    @Override
    public View getLayoutview() {
        return View.inflate(getActivity(), R.layout.d_abstract, null);
    }

    @Override
    public void requestFailure(Request request, IOException e) {

    }

    @Override
    public void requestSuccess(String result) throws Exception {
        relationlistbean = new ArrayList<>();
        JSONObject jsonObject = new JSONObject(result);
        JSONObject data = jsonObject.optJSONObject("data");
        String copyright = data.optString("copyright");
        if (copyright.equals("3")) {
            details_bavean3 = new Gson().fromJson(result, details_avBean3.class);
            setview3();
        }else if(copyright.equals("2")){
            details_avbean2 = new Gson().fromJson(result, details_avBean2.class);
            setview2();
        }else {
            details_avbean = new Gson().fromJson(result, details_avBean.class);
            setview();
        }
        jsonObject = null;
        recy.setLayoutManager(new LinearLayoutManager(getContext()));
        recy.setAdapter(new d_abstractAdapter(getContext(),relationlistbean));
    }

    private void setview3() {
        title.setText(details_bavean3.getData().getTname());
        b_number.setText(details_bavean3.getData().getAttribute()+"人/次");
        p_number.setText(details_bavean3.getData().getAid()+"人/次");
        number_fx.setText(details_bavean3.getData().getStat().getShare()+"");
        number_tb.setText(details_bavean3.getData().getStat().getCoin()+"");
        number_sc.setText(details_bavean3.getData().getStat().getFavorite()+"");
        img.setImageURI(details_bavean3.getData().getOwner().getFace());
        name.setText(details_bavean3.getData().getOwner().getName());

        List<details_avBean3.DataBean.RelatesBean> relates = details_bavean3.getData().getRelates();

        for (int i = 0; i < relates.size()-1; i++) {
            details_relationBean bean = new details_relationBean();
            relationlistbean.add(bean);
            relationlistbean.get(i).setPic(relates.get(i).getPic());
            relationlistbean.get(i).setName(relates.get(i).getOwner().getName());
            relationlistbean.get(i).setTitle(relates.get(i).getTitle());
            relationlistbean.get(i).setFavorite(relates.get(i).getStat().getFavorite()+"");
            relationlistbean.get(i).setReply(relates.get(i).getStat().getReply()+"");
            bean = null;
            System.gc();
        }
    }

    private void setview2() {
        title.setText(details_avbean2.getData().getTname());
        b_number.setText(details_avbean2.getData().getAttribute()+"人/次");
        p_number.setText(details_avbean2.getData().getAid()+"人/次");
        number_fx.setText(details_avbean2.getData().getStat().getShare()+"");
        number_tb.setText(details_avbean2.getData().getStat().getCoin()+"");
        number_sc.setText(details_avbean2.getData().getStat().getFavorite()+"");
        img.setImageURI(details_avbean2.getData().getOwner().getFace());
        name.setText(details_avbean2.getData().getOwner().getName());

        List<details_avBean2.DataBean.RelatesBean> relates = details_avbean2.getData().getRelates();
        for (int i = 0; i < relates.size()-1; i++) {
            details_relationBean bean = new details_relationBean();
            relationlistbean.add(bean);
            relationlistbean.get(i).setPic(relates.get(i).getPic());
            relationlistbean.get(i).setName(relates.get(i).getOwner().getName());
            relationlistbean.get(i).setTitle(relates.get(i).getTitle());
            relationlistbean.get(i).setFavorite(relates.get(i).getStat().getFavorite()+"");
            relationlistbean.get(i).setReply(relates.get(i).getStat().getReply()+"");
            bean = null;
            System.gc();
        }
    }

    private void setview() {
        title.setText(details_avbean.getData().getTname() + "");
        b_number.setText(details_avbean.getData().getAttribute()+"人/次");
        p_number.setText(details_avbean.getData().getAid()+"人/次");
        number_fx.setText(details_avbean.getData().getStat().getShare()+"");
        number_tb.setText(details_avbean.getData().getStat().getCoin()+"");
        number_sc.setText(details_avbean.getData().getStat().getFavorite()+"");
        img.setImageURI(details_avbean.getData().getOwner().getFace());
        name.setText(details_avbean.getData().getOwner().getName());

        List<details_avBean.DataBean.RelatesBean> relates = details_avbean.getData().getRelates();
        for (int i = 0; i < relates.size()-1; i++) {
            details_relationBean bean = new details_relationBean();
            relationlistbean.add(bean);
            relationlistbean.get(i).setPic(relates.get(i).getPic());
            relationlistbean.get(i).setName(relates.get(i).getOwner().getName());
            relationlistbean.get(i).setTitle(relates.get(i).getTitle());
            relationlistbean.get(i).setFavorite(relates.get(i).getStat().getFavorite()+"");
            relationlistbean.get(i).setReply(relates.get(i).getStat().getReply()+"");
            bean = null;
            System.gc();
        }
    }
}
